<?php

namespace haibao\cms\business;

use haibao\cms\data\Icon as IconData;
use haibao\frame\data\query\Condition;
use haibao\cms\business\ArticleTag as ArticleTagBusiness;
use haibao\cms\business\Article as ArticleBusiness; 

class Icon extends \haibao\cms\business\Base{

    public static function getIconList($filter,$isGetOne = true){
        $iconData = new IconData();
        if($isGetOne){
            $data = $iconData->getOne($filter);
        }else{
            $data = $iconData->getAll($filter);
        }
        return $data;
    }
    
    public static function delIconById($id, $arrSet){
        self::checkOperation(self::FUNC_CODE_ICON_DEL);
        if (!$id){
            throw new BusinessException('ID不能为空');
        }
        $iconData = new IconData();
        $iconData->updateIconDataById($id,$arrSet);
        self::saveOperateHistory(self::FUNC_CODE_ICON_DEL, array($id,$arrSet));
    }
    
    public static function saveIconPraiseNumById($id,$praiseNum){
        self::checkOperation(self::FUNC_CODE_ICON_EDIT);
        if (!$id){
            throw new BusinessException('ID不能为空');
        }
        $iconData = new IconData();
        $iconData->updateIconDataById($id,$praiseNum);
        self::saveOperateHistory(self::FUNC_CODE_ICON_EDIT, array($id,$praiseNum));
    }
    
    public static function getIconListById($id){
        $iconData = new IconData();
        return $iconData->getOneById($id);
    }
    
    public static function addIcon($model){
        self::checkOperation(self::FUNC_CODE_ICON_ADD);
        self::checkIconModelData($model);
        $iconData = new IconData();
        $iconData->addIconInfo($model);
        self::saveOperateHistory(self::FUNC_CODE_ICON_ADD, $model->toArray(true,true));
    }
    
    public static function editIcon($model){
        self::checkOperation(self::FUNC_CODE_ICON_EDIT);
        self::checkIconModelData($model);
        $iconData = new IconData();
        $iconData->updateIconModelData($model);
        self::saveOperateHistory(self::FUNC_CODE_ICON_EDIT, array('id'=>$model->Id));
    }
    
    private static function checkIconModelData($model){
        if(!$model){
            throw new BusinessException('error');
        }
    }
    
    public static function getArtilcePrimaryIdByArticleId($articleId){
        $artilceTagFilter = new \haibao\cms\model\filter\articleTag();
        $artilceTagFilter->select(array(
            \haibao\cms\model\filter\articleTag::CONDITION_FIELD_NAME_ID,
            \haibao\cms\model\filter\articleTag::CONDITION_FIELD_NAME_ARTICLE_ID,
            \haibao\cms\model\filter\articleTag::CONDITION_FIELD_NAME_IS_PRIMAIY,
            \haibao\cms\model\filter\articleTag::CONDITION_FIELD_NAME_PARENT_ID,
        ));
        $artilceTagFilter->where(\haibao\cms\model\filter\articleTag::CONDITION_FIELD_NAME_ARTICLE_ID, Condition::CONDITION_EQUAL, (int)$articleId);
        $artilceTagFilter->where(\haibao\cms\model\filter\articleTag::CONDITION_FIELD_NAME_IS_PRIMAIY, Condition::CONDITION_EQUAL, 1);
        $result = ArticleTagBusiness::getAllTags($artilceTagFilter,TRUE);
        return $result; 
    }
    public static function getArtilcePublishIdByArticleId($articleId){
        $result = ArticleBusiness::getArticleById($articleId);
        return $result; 
    }
    
    public static function getPicPrimaryIdByPicId($articleId){
        $iconData = new IconData();
        return $iconData->getPicPrimaryIdByPicId($articleId);
    }
    public static function getPicTitleByArticleId($articleId){
        $iconData = new IconData();
        return $iconData->getPicTitleByArticleId($articleId);
    }
    
    public static function exportData(){
        $iconData = new IconData();
        $iconData->exportData();
    }
}